import requests
import re

def login():
    # session
    session = requests.session()

    #header
    session.headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36'
    }

    # url1-获取token
    url1 = 'https://github.com/login'
        # 发送请求获取响应
    res_1 = session.get(url1).content.decode()
        # 正则提取
    token = re.findall('name="authenticity_token" value="(.*?)" />', res_1)
    print(token)

    # url2-登陆
    url2 = 'https://github.com/session'
        # 构建表单数据
    data = {
        'commit': 'Sign in',
        'authenticity_token': token,
        'ga_id': '',
        'login': 'username',
        'password': 'password',
        'webauthn-support': 'supported'
    }
    print(data)

        # 发送请求登陆
    session.post(url2, data=data)
    # url3-验证
    url3 = 'https://github.com/Yjf2000'
    response = session.get(url3)
    with open('github.html', 'wb')as f:
        f.write(response.content)

if __name__ == '__main__':
    login()